<?php
/**
 * 论坛模块
 */
if(!defined("Yourphp")) exit("Access Denied");
class BbsAction extends BaseAction
{
    //社区主页
    public function index(){
        import('@.ORG.Page');
        if(IS_POST){
            $like_keywords = array('like','%'.$_POST['keywords'].'%');
            $where = array('title'=>$like_keywords,'content'=>$like_keywords,'username'=>$like_keywords,'_logic'=>'or');
            $count = M('sq_tips')->where($where)->count();
            $page = new Page($count, 10);
            $show = $page->show();
            $list = M('sq_tips')->where($where)->order('time DESC')->limit($page->firstRow . ',' . $page->listRows)->select();
        }else{
            $count = M('sq_tips')->count();
            $page = new Page($count, 10);
            $show = $page->show();
            $list = M('sq_tips')->order('time DESC')->limit($page->firstRow . ',' . $page->listRows)->select();
            $top = M('sq_tips')->where(array('is_top'=>1,'is_re'=>1,'_logic'=>'or'))->select();
            foreach($top as $k=>$v){
                $top[$k]['img'] = $this->get_img($v['uid'],$v['type']);
                $top[$k]['username'] = $this->get_username($v['uid'],$v['type']);
            }
        }
        foreach($list as $k=>$v){
            $list[$k]['img'] = $this->get_img($v['uid'],$v['type']);
            $list[$k]['username'] = $this->get_username($v['uid'],$v['type']);
        }
        $this->assign('p',$_GET['p']);
        $hot = M('sq_tips')->order('posts DESC')->limit(10)->select();
        $this->assign("page", $show);
        $this->assign('list',$list);
        $this->assign('uid',$this->_userid);
        $this->assign('u_img',$this->get_img($this->_userid,$this->_ms));
        $this->assign('top',$top);
        $this->assign('hot',$hot);
        $this->display();
    }
    //帖子发表&编辑
    public function add(){
        if(!$this->_userid){
            echo "<script>alert('不好意思，请先登录！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }
        if($_GET['id'] != ''){
            $tips = M('sq_tips')->where(array('id'=>$_GET['id']))->find();
            if($tips['uid'] != $this->_userid){
                echo "<script>alert('没有权限，非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }
            $this->assign('tips',$tips);
        }
        $this->assign('uid',$this->_userid);
        $this->assign('ms',$this->_ms);
        $this->display();
    }
    //帖子保存
    public function save(){
        if(IS_POST){
        $data = $_POST;
        $data['uid'] = $this->_userid;
        $data['time'] = time();
        $data['type'] = $this->_ms;
        if($_POST['id']){
            if(M('sq_tips')->where(array('id'=>$_POST['id']))->save($data)){
                echo "<script>alert('修改成功！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }else{
                echo "<script>alert('修改失败！');history.back();</script>";
            }
        }else{
            if(M('sq_tips')->add($data)){
                echo "<script>alert('发表成功！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }else{
                echo "<script>alert('发表失败！');history.back();</script>";
            }
        }
        }else{
            echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }

    }
    //删除帖子
    public function del_tips(){
        if($_GET['id']){
            $posts = M('sq_com')->where(array('tid'=>$_GET['id']))->delete();
            $del = M('sq_tips')->where(array('id'=>$_GET['id']))->delete(); 
            if($del){
                echo "<script>alert('删除帖子成功！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }else{
                echo "<script>alert('删除帖子失败！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }
        }else{
            echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }
    }
    //帖子详情
    public function show(){
        import('@.ORG.Page');
        if($_GET['id'] != ''){
            $hot = M('sq_tips')->order('posts DESC')->limit(10)->select();
            $tips_info = M('sq_tips')->where(array('id'=>$_GET['id']))->find();
            $tips_info['img'] = $this->get_img($tips_info['uid'],$tips_info['type']);
            $tips_info['username'] = $this->get_username($tips_info['uid'],$tips_info['type']);
            $com_count = M('sq_com')->where(array('tid'=>$_GET['id']))->count();
            $page = new Page($com_count, 5);
            $show = $page->show();
            $tips_com = M('sq_com')->where(array('tid'=>$_GET['id']))->limit($page->firstRow . ',' . $page->listRows)->select();
            foreach($tips_com as $k=>$v){
                $tips_com[$k]['img'] = $this->get_img($v['uid'],$v['type']);
                $tips_com[$k]['username'] = $this->get_username($v['uid'],$v['type']);
            }
            $this->assign('tips_info',$tips_info);
            $this->assign('tips_com',$tips_com);
            $this->assign('page',$show);
            $this->assign('p',$_GET['p']);
            $this->assign('hot',$hot);
            $this->assign('ms',$this->_ms);
            $this->assign('uid',$this->_userid);
            $this->display();
        }else{
            echo "<script>alert('不好意思，该帖子不存在！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }
    }
    //评论保存
    public function addcom(){
        if(IS_POST){
            $data = $_POST;
            $data['uid'] = $this->_userid;
            $data['time'] = time();
            $data['type'] = $this->_ms;
            $tid = $data['tid'];
            if($_POST['id']){
                if(M('sq_com')->where(array('id'=>$_POST['id']))->save($data)){
                    echo "<script>alert('修改成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$tid';</script>";
                }else{
                    echo "<script>alert('修改失败！');history.back();</script>";
                }
            }else{
                if(M('sq_com')->add($data)){
                    M('sq_tips')->where(array('id'=>$tid))->setInc('posts',1);
                    echo "<script>alert('发表成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$tid';</script>";
                }else{
                    echo "<script>alert('发表失败！');history.back();</script>";
                }
            }
            }else{
                echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }
    }
    //评论删除
    public function del_com(){
        if($_GET['id']){
            $posts = M('sq_tips')->where(array('id'=>$_GET['tid']))->setDec('posts',1);
            $del = M('sq_com')->where(array('id'=>$_GET['id']))->delete();
            $id = $_GET['tid'];
            if($del){
                echo "<script>alert('删除评论成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
            }else{
                echo "<script>alert(删除评论失败！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
            }
        }else{
            echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }
    }
    //帖子评论修改页面
    public function edit(){
        if($_GET['id'] == ''){
            echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }else{
            $info = M('sq_com')->where(array('id'=>$_GET['id']))->find();
            if($info['uid'] != $this->_userid){
                echo "<script>alert('没有权限！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
            }
            $this->assign('info',$info);
            $this->display();
        }
    }
    //用户中心
    public function center(){
        $uid = $_GET['uid'];
        $type = $_GET['type'];
        if($uid && $type){       
            $user = $this->get_username($uid,$type);
            if($uid == $this->_userid){
                $tips = M('sq_tips')->where(array('uid'=>$uid,'type'=>$type))->order('time DESC')->select();
                $com = M('sq_com')->where(array('uid'=>$uid,'type'=>$type))->order('time DESC')->select();
            }else{
                $tips = M('sq_tips')->where(array('uid'=>$uid,'type'=>$type))->order('time DESC')->limit(5)->select();
                $com = M('sq_com')->where(array('uid'=>$uid,'type'=>$type))->order('time DESC')->limit(5)->select();
            }    
            foreach($com as $k=>$v){
                $com[$k]['title'] = M('sq_tips')->where(array('id'=>$v['tid']))->getField('title');
            }
            $this->assign('com',$com);
            $this->assign('tips',$tips);
            $this->assign('user',$user);
        }
        $this->display();
    }
    //置顶
    public function top(){
        $id = $_GET['id'];
        if($id != ''){
            $top = M('sq_tips')->where(array('id'=>$_GET['id']))->getField('is_top');
            if($top == 1){
                if(M('sq_tips')->where(array('id'=>$_GET['id']))->save(array('is_top'=>0))){
                    echo "<script>alert('取消置顶成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }else{
                    echo "<script>alert('取消置顶失败！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }
            }else{
                if(M('sq_tips')->where(array('id'=>$_GET['id']))->save(array('is_top'=>1))){
                    echo "<script>alert('置顶成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }else{
                    echo "<script>alert('置顶失败！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }
            }
        }else{
            echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }
    }
    //加精
    public function re(){
        $id = $_GET['id'];
        if($id != ''){
            $re= M('sq_tips')->where(array('id'=>$_GET['id']))->getField('is_re');
            if($re == 1){
                if(M('sq_tips')->where(array('id'=>$_GET['id']))->save(array('is_re'=>0))){
                    echo "<script>alert('取消精华成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }else{
                    echo "<script>alert('取消精华失败！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }
            }else{
                if(M('sq_tips')->where(array('id'=>$_GET['id']))->save(array('is_re'=>1))){
                    echo "<script>alert('加精成功！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }else{
                    echo "<script>alert('加精失败！');window.location.href='/index.php?g=Home&m=Bbs&a=show&id=$id';</script>";
                }
            }
        }else{
            echo "<script>alert('非法访问！');window.location.href='/index.php?g=Home&m=Bbs&a=index';</script>";
        }
    }
    public function get_img($id,$type){
        $data = $this->http('http://user.fjhgkj.com.cn/index.php?m=Home&c=Index&a=get_img',array('id'=>$id,'type'=>$type));
        return $data;
    }
    public function get_username($id,$type){
        $name= json_decode($this->http('http://user.fjhgkj.com.cn/index.php?m=Home&c=Index&a=get_username',array('id'=>$id,'type'=>$type)),true)['name'];
        return $name;
    }
}
